package com.zwz.mall.app;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.zwz.mall.R;
import com.zwz.mall.header.LoadingHeader;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * @Author: EzerXie
 * @Description:
 * @Date: Created on 2018/11/5 01:04
 * @Modified By:
 */
public abstract class BaseApplication extends Application
{

    private static BaseApplication mApp;

    public static BaseApplication getApplication()
    {
        return mApp;
    }

    public abstract String getHost();

    @Override
    public void onCreate()
    {
        super.onCreate();
        closeAndroidPDialog();
        mApp = this;
    }

    @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    //去掉9.0弹框提醒
    private void closeAndroidPDialog()
    {
        try
        {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
/*
static 代码段可以防止内存泄露
下拉刷新,一般添加调用接口获取数据的方法
上拉加载，一般添加调用接口获取更多数据的方法
 */
    static
    {
                //设置全局的Header构建器
                SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator()
                {
                    @Override
                    public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout)
                    {
//                        layout.setPrimaryColorsId(R.color.colorPrimary);//全局设置主题颜色
                        MaterialHeader materialHeader = new MaterialHeader (context);
//                        LoadingHeader loadingHeader = new LoadingHeader(context);
//                        classicsHeader.setEnableLastTime(false);
                        return materialHeader;
                    }
                });
                //设置全局的Footer构建器
                SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator()
                {
                    @Override
                    public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout)
                    {
                        return new ClassicsFooter(context);
                    }
                });
    }
}
